package com.lanhai.crowd.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanhai.crowd.entity.Role;
import com.lanhai.crowd.entity.RoleExample;
import com.lanhai.crowd.mapper.RoleMapper;
import com.lanhai.crowd.service.api.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageInfo<Role> getPageInfo(String keyword, Integer pageNum, Integer pageSize) {

        // 1.开始分页
        PageHelper.startPage(pageNum, pageSize);

        // 2.查询
        List<Role> roles = roleMapper.selectRoleByKeyword(keyword);

        // 3.返回PageInfo
        return new PageInfo<>(roles);
    }

    @Override
    public void saveRole(Role role) {
        roleMapper.insert(role);
    }

    @Override
    public void editRole(Role role) {
        roleMapper.updateByPrimaryKey(role);
    }

    @Override
    public void deleteRoleByIds(List<Integer> ids) {

        RoleExample roleExample = new RoleExample();

        RoleExample.Criteria criteria = roleExample.createCriteria();

        criteria.andIdIn(ids);

        roleMapper.deleteByExample(roleExample);
    }

    @Override
    public List<Role> getAssignedRole(Integer adminId) {

        List<Role> roles = roleMapper.selectAssignedRole(adminId);
        return roles;
    }

    @Override
    public List<Role> getUnassignedRole(Integer adminId) {

        List<Role> roles = roleMapper.selectUnassignedRole(adminId);
        return roles;
    }

    @Override
    public void saveAdminRoleRelationship(Integer adminId, List<Integer> roleIdList) {
        // 1.删除旧的用户角色关系
        roleMapper.deleteOldAdminRoleRelationship(adminId);

        // 2.新增新的用户角色关系
        if(null != roleIdList && roleIdList.size() > 0){
            roleMapper.insertNewAdminRoleRelationship(adminId, roleIdList);

        }

    }
}
